﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SortableCollectionComponent;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
using System.Xml.Serialization;
using System.IO;
using WPFSortableCollectionTest.Model;

namespace WPFSortableCollectionTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        #region store for people

        private SortableObservableCollection<Person> people;

        public SortableObservableCollection<Person> People
        {
            get { return people; }
            set
            {
                people = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("People"));
            }
        }

        public SortableCollectionComponent.SortDescription FirstNameSort { get; set; }
        public SortableCollectionComponent.SortDescription LastNameSort { get; set; }

        #endregion

        #region constructor

        public Window1()
        {
            InitializeComponent();

            FirstNameSort = new SortableCollectionComponent.SortDescription() { PropertyName = "FirstName", Direction = SortDirection.Descending };
            LastNameSort = new SortableCollectionComponent.SortDescription() { PropertyName = "LastName", Direction = SortDirection.Descending };

            People = new SortableObservableCollection<Person>();

            //creates 140,000ish records
            List<Person> data = Person.GetPeople();

            List<Person> data2 = Person.GetPeople();

            data.AddRange(data2);

            List<Person> data3 = Person.GetPeople();

            data.AddRange(data3);

            List<Person> data4 = Person.GetPeople();

            data.AddRange(data4);

            List<Person> data5 = Person.GetPeople();

            data.AddRange(data5);

            List<Person> data6 = Person.GetPeople();

            data.AddRange(data6);

            List<Person> data7 = Person.GetPeople();

            data.AddRange(data7);

            Action<Person> addAction = (person) =>
                {
                    People.Add(person);
                };

            data.ForEach(addAction);

        }

        #endregion

        #region event handlers
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void Sort_Click(object sender, RoutedEventArgs e)
        {
            Button sort = (Button)sender;

            string header = sort.Content.ToString();

            People.SortDescriptions.Clear();

            if (header == "First Name")
            {
                if (FirstNameSort.Direction == SortDirection.Descending)
                    FirstNameSort.Direction = SortDirection.Ascending;
                else
                    FirstNameSort.Direction = SortDirection.Descending;

                People.SortDescriptions.Add(FirstNameSort);
            }
            else
            {
                if (LastNameSort.Direction == SortDirection.Descending)
                    LastNameSort.Direction = SortDirection.Ascending;
                else
                    LastNameSort.Direction = SortDirection.Descending;

                People.SortDescriptions.Add(LastNameSort);
            }

            People.RefreshSort();
        }
    }
}
